home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Interfaces / UniversalInterfaces 1.0 / CIncludes / complex.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-18  |  7.6 KB  |  252 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Complex.h
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Written by:    Jeff Cobb
  7.  
  8.     Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>      3/4/93    jrc        Total rewrite from Ali (so people will stop bothering him about
  13.                                     our old headers)
  14.  
  15. */
  16.  
  17. /************************************************************
  18.  
  19. Created: Monday, June 27, 1988 12:42:53 PM
  20.     Complex.h
  21.     C Interface to the Macintosh Libraries
  22.  
  23.  
  24.     Copyright Apple Computer, Inc. 1985-1992
  25.     All rights reserved.
  26.     
  27. IMPORTANT NOTE
  28.  
  29. PowerPC will for the time being support only one complex type
  30. composed of an ordered pair of double_t (double) values. NCEG has not
  31. yet specified complex extensions to C.  Without such a specification,
  32. support for a second wider (long double) complex type will introduce
  33. complications in mixed precision calculations, overloading in C++,
  34. etc., as well as poorer performance.  Since double_t is defined to
  35. be long double (extended) on the Motorola 680X0 Macintosh, the complex
  36. arithmetic implementation remains the same in that world.
  37.  
  38. CHANGE LOG:
  39.  
  40.     30 Oct 92  JPO    Changed all instances of "extended" to "double_t"
  41.                     to accommodate PowerPC.
  42.                     
  43.     August 25 1993   ali   changed clog to cLog to avoid clashing with
  44.                            the stream i/o clog.
  45.  
  46. ************************************************************/
  47.  
  48. #ifndef __COMPLEX__
  49. #define __COMPLEX__
  50.  
  51. #ifndef __FP__
  52. #include "fp.h"
  53. #endif
  54.  
  55. #ifndef   powerc              /*   preserve compatibility with 68k macintoshes    */
  56. #define   cLog    clog
  57. #endif
  58.  
  59. struct complex;
  60.  
  61. /*
  62.     NOTE to users of the complex class stream i/o functionality:
  63.     
  64.     In order to use the complex stream functionality prototyped by the two following 
  65.     function declarations one must include either <IOStream.h> or <Stream.h> before
  66.     including <Complex.h>.
  67.  
  68. */
  69. #ifdef __IOSTREAM__
  70. ostream& operator<<(ostream&, complex);
  71.     istream& operator>>(istream&, complex&);
  72. #endif
  73.     
  74. struct complex {
  75.     double_t    re;
  76.     double_t    im;
  77.     
  78. #ifdef __cplusplus
  79.  
  80.         complex() { re =0.0; im =0.0; }
  81.         complex(double_t r, double_t i =0.0) { re =r; im =i; }
  82.  
  83.     friend double_t real(const complex);
  84.     friend double_t imag(const complex);
  85.     friend double_t abs(complex);
  86.     friend double_t norm(complex);
  87.     friend double_t arg(complex);
  88.  
  89.     friend complex    acos(complex);
  90.     friend complex    acosh(complex);
  91.     friend complex    asin(complex);
  92.     friend complex    asinh(complex);
  93.     friend complex    atan(complex);
  94.     friend complex    atanh(complex);
  95.     friend complex    conj(complex);
  96.     friend complex    cos(complex);
  97.     friend complex    cosh(complex);
  98.     friend complex    exp(complex);
  99.     friend complex    log(complex);
  100.     friend complex    pow(complex, complex);
  101.     friend complex    pow(complex, long);
  102.     friend complex    pow(complex, double_t);
  103.     friend complex    pow(double_t, complex);
  104.     friend complex    polar(double_t, double_t);
  105.     friend complex    sin(complex);
  106.     friend complex    sinh(complex);
  107.     friend complex    sqrt(complex);
  108.     friend complex    sqr(complex);
  109.     friend complex    tan(complex);
  110.     friend complex    tanh(complex);
  111.     friend complex    operator +(complex, complex);
  112.     friend complex    operator -(complex, complex);
  113.     friend complex    operator -(complex);
  114.     friend complex    operator *(complex, complex);    
  115.     friend complex    operator *(complex, double_t);
  116.     friend complex    operator *(double_t, complex);
  117.     friend complex    operator /(complex, complex);
  118.     friend complex    operator /(complex, double_t);
  119.     friend complex    operator /(double_t, complex);
  120.     friend int        operator ==(complex, complex);
  121.     friend int        operator !=(complex, complex);
  122.     complex     operator +=(complex);
  123.     complex     operator -=(complex);
  124.     complex     operator *=(complex);
  125.     complex     operator *=(double_t);
  126.     complex     operator /=(complex);
  127.     complex     operator /=(double_t);
  128.  
  129. #endif
  130. };
  131.  
  132.  
  133.  
  134. #ifndef __cplusplus
  135. typedef struct complex complex;
  136. #else
  137. extern "C" {
  138. #endif
  139.     
  140. complex cadd( complex x, complex y );
  141. complex csub( complex x, complex y );
  142. complex cmul( complex x, complex y );
  143. complex cdiv( complex x, complex y );
  144. complex xdivc( double_t x, complex y );
  145. complex csqrt( complex z );
  146. complex csin( complex z );
  147. complex ccos( complex z );
  148. complex csquare( complex z );
  149. complex cexp( complex z );
  150. complex cLog( complex z );
  151. complex cepwry( double_t x, complex y );
  152. complex cxpwri( complex x, long y );
  153. complex cxpwre( complex x, double_t y );
  154. complex cxpwry( complex x, complex y );
  155. complex csinh( complex z );
  156. complex ccosh( complex z );
  157. complex ctanh( complex z );
  158. complex ctan( complex z );
  159. complex casin( complex z );
  160. complex casinh( complex z );
  161. complex cacos( complex z );
  162. complex cacosh( complex z );
  163. complex catan( complex z );
  164. complex catanh( complex z );
  165. complex cconj( complex z );
  166.  
  167. double_t cabs( complex z );
  168. double_t carg( complex z );
  169.  
  170. #ifdef __cplusplus
  171. }    // close the extern "C" declaration
  172.  
  173. inline double_t    real(const complex a)    { return a.re; }
  174. inline double_t imag(const complex a)    { return a.im; }
  175. inline double_t abs(complex a)    { return cabs(a); }
  176. inline double_t norm(complex a)    { return a.re*a.re+a.im*a.im; }
  177. inline double_t arg(complex a)    { return carg(a); }
  178. inline complex    acos(complex a)    { return cacos(a); }
  179. inline complex    acosh(complex a)    { return cacosh(a); }
  180. inline complex    asin(complex a)    { return casin(a); }
  181. inline complex    asinh(complex a)    { return casinh(a); }
  182. inline complex    atan(complex a)    { return catan(a); }
  183. inline complex    atanh(complex a)    { return catanh(a); }
  184. inline complex    conj(complex a)    { return complex(a.re, -a.im); }
  185. inline complex    cos(complex a)    { return ccos(a); }
  186. inline complex    cosh(complex a)    { return ccosh(a); }
  187. inline complex    exp(complex a)    { return cexp(a); }
  188. inline complex    log(complex a)    { return cLog(a); }
  189. inline complex    pow(complex a, complex b)    { return cxpwry(a, b); }
  190. inline complex    pow(complex a, long b)    { return cxpwri(a, b); }
  191. inline complex    pow(complex a, double_t b)    { return cxpwre(a, b); }
  192. inline complex    pow(double_t a, complex b) { return cepwry(a, b); }
  193. inline complex    polar(double_t r, double_t theta)    { return complex(r*cos(theta), r*sin(theta) ); }
  194. inline complex    sin(complex a)    { return csin(a); }
  195. inline complex    sinh(complex a)    { return csinh(a); }
  196. inline complex    sqrt(complex a)    { return csqrt(a); }
  197. inline complex    sqr(complex a)    { return csquare(a); }
  198. inline complex    tan(complex a)    { return ctan(a); }
  199. inline complex    tanh(complex a)    { return ctanh(a); }
  200. inline complex    operator +(complex a, complex b)    { return complex(a.re+b.re, a.im+b.im); }
  201. inline complex    operator -(complex a,complex b)    { return complex(a.re-b.re, a.im-b.im); }
  202. inline complex    operator -(complex a)    { return complex(-a.re, -a.im); }
  203. inline complex    operator *(complex a, complex b)    { return cmul(a, b); }    
  204. inline complex    operator *(complex a, double_t b)    { return complex(a.re*b, a.im*b); }
  205. inline complex    operator *(double_t a, complex b)    { return complex(a*b.re, a*b.im); }
  206. inline complex    operator /(complex a, complex b)    { return cdiv(a, b); }
  207. inline complex    operator /(complex a, double_t b) { return complex(a.re/b, a.im/b); }
  208. inline complex    operator /(double_t a, complex b)    { return xdivc(a, b); }
  209. inline int        operator ==(complex a, complex b)    { return (a.re==b.re && a.im==b.im); }
  210. inline int        operator !=(complex a, complex b)    { return (a.re!=b.re || a.im!=b.im); }
  211.  
  212. inline complex complex::operator +=(complex a)
  213. {
  214.     re += a.re;
  215.     im += a.im;
  216.     return complex(re, im);
  217. }
  218.  
  219. inline complex complex::operator -=(complex a)
  220. {
  221.     re -= a.re;
  222.     im -= a.im;
  223.     return complex(re, im);
  224. }
  225.  
  226. inline complex complex::operator *=(complex a)
  227. {
  228.     return *this = cmul(*this, a);
  229. }
  230.  
  231. inline complex complex::operator *=(double_t a)
  232. {
  233.     re *= a;
  234.     im *= a;
  235.     return complex(re, im);
  236. }
  237.  
  238. inline complex complex::operator /=(complex a)
  239. {
  240.     return *this = cdiv(*this, a);
  241. }
  242.  
  243. inline complex complex::operator /=(double_t a)
  244. {
  245.     re /= a;
  246.     im /= a;
  247.     return complex(re, im);
  248. }
  249.  
  250. #endif
  251.  
  252. #endif